/**
 * 
 */
package xx.services.teach;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.services.Passport;
import xx.services.util.Constants;
import xx.teach.entity.Meno;
import xx.teach.entity.Schedule;

/**
 * @author Administrator 2014-3-28
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/teach/schedule")
public class ScheduleRest extends Passport
{

    /**
     * @param request
     */
    public ScheduleRest(@Context
    HttpServletRequest request)
    {
        super(request);
    }

    /**
     * 老师上课，更新上课时间表状态
     * 
     * @param groupId
     * @return
     */
    @POST
    @Path("/group/{group_id}/onclass")
    public String setStatusOnclass(@PathParam("group_id")
    long groupId)
    {
        try
        {
            int count = 0;
            Schedule schedule = new Schedule(qr, owner);
            count = schedule.setGroupId(groupId).updateStatusOnClass();
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 老师下课，更新时间表状态
     * 
     * @param groupId
     * @return
     */
    @POST
    @Path("/group/{group_id}/downclass")
    public String setStatusDownclass(@PathParam("group_id")
    long groupId)
    {
        try
        {
            int count = 0;
            Schedule schedule = new Schedule(qr, owner);
            count = schedule.setGroupId(groupId).updateStatusDownClass();
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 学生上课，插入到达时间
     * 
     * @param groupId
     * @return
     */
    @POST
    @Path("/account/{account_id}/onclass")
    public String setArriveTime(@PathParam("account_id")
    long accountId)
    {
        try
        {
            int count = 0;
            Schedule schedule = new Schedule(qr, owner);
            count = schedule.setAccountId(accountId).updateArriveTimeByStudent();
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 学生上课，插入到达时间
     * 
     * @param groupId
     * @return
     */
    @POST
    @Path("/account/{account_id}/downclass")
    public String setLeaveTime(@PathParam("account_id")
    long accountId)
    {
        try
        {
            int count = 0;
            Schedule schedule = new Schedule(qr, owner);
            count = schedule.setAccountId(accountId).updateLeaveTimeByStudent();
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 统计已经签到的名单
     * 
     * @param groupId
     * @return
     */
    @GET
    @Path("/group/{group_id}/calculate")
    public String calculate(@PathParam("group_id")
    long groupId)
    {
        try
        {
            Schedule schedule = new Schedule(qr, owner);
            schedule.setGroupId(groupId);
            RowSet rs = schedule.calculate();
            RowSet rsLate = schedule.calculateLate();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("late", rsLate);
            return ok(rs, map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
}
